<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f2f2f2;
        }
        .container {
            width: 400px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            background-color: #fff;
        }
        .container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input[type="text"],
        .form-group input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .form-group input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .form-group input[type="submit"]:hover {
            background-color: #45a049;
        }
        .error {
            color: red;
            margin-top: -10px;
            margin-bottom: 10px;
            font-size: 14px;
        }
        .link {
            text-align: center;
            margin-top: 20px;
        }
        .link a {
            color: #0066cc;
            text-decoration: none;
        }
        .link a:hover {
            text-decoration: underline;
        }
    </style>
    <script>
        function validateForm(event) {
            event.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorContainer = document.getElementById('error-container');

            const xhr = new XMLHttpRequest();
            xhr.open("POST", "LoginServlet", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    const response = JSON.parse(xhr.responseText);
                    if (response.status === "error") {
                        errorContainer.textContent = response.message;
                    } else if (response.status === "success") {
                        window.location.href = "home.jsp";
                    }
                }
            };
            xhr.send("username=" + encodeURIComponent(username) + "&password=" + encodeURIComponent(password));
        }
    </script>
</head>
<body>
    <div class="container">
        <h2>用户登录</h2>
        <form onsubmit="validateForm(event)">
            <div class="form-group">
                <label for="username">账户（邮箱）：</label>
                <input type="text" id="username" name="username" required />
            </div>
            <div class="form-group">
                <label for="password">密码：</label>
                <input type="password" id="password" name="password" required />
            </div>
            <div class="error" id="error-container"></div>
            <div class="form-group">
                <input type="submit" value="登录" />
            </div>
        </form>
        <div class="link">
            <a href="register.jsp">注册</a>
            <a href="forgot-password.jsp">找回密码</a>
        </div>
    </div>
</body>
</html>
